package com.fashion.web.actions.management.gallery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.gallery.Album;
import com.fashion.model.gallery.GalleryColumn;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.gallery.GalleryColumnService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("json-default")
@Namespace("/management/gallery")
public class GalleryAdminAction extends ActionSupport {
    
    private List<ColumnComboTreeModel> columnsComboTree;
	private List<Album> albums;
	private List<AlbumDataGridModel> albumsDataGrid;
	Map result = new HashMap();
	private Album album;
	@Resource(name = "albumService")
	private AlbumService albumService;
	@Resource(name = "galleryColumnService")
    private GalleryColumnService columnService;
	private String galleryTitle;
	private String galleryColumn;
	private String order ="priority";
	private int rows;  
    private int page;  
	
	@Action(value = "find_albums", results = {@Result(name = SUCCESS, type = "json", params = {"root", "result"})})
	public String find() throws Exception {
		int total = albumService.count(galleryTitle, galleryColumn);
		albums = albumService.find(galleryTitle, galleryColumn, order, rows*(page - 1 ), rows);
		result.put("total", total);
		result.put("rows", AlbumDataGridModel.getInstances(albums));
		return SUCCESS;
	}
	
	@Action(value = "find_column_combotree", results = {@Result(name = SUCCESS, type = "json", params = {"root", "columnsComboTree"})})
    public String ComboTree() throws Exception{
        List<GalleryColumn> columns = columnService.format(columnService.findAll());
        columnsComboTree = ColumnComboTreeModel.getInstances(columns);
        return SUCCESS;
    }
	
	@Action(value = "add_or_update_album", results = {@Result(name = SUCCESS, location = "gallery.jsp")})
	public String add() throws Exception {
		if(album.getId().equals("-1")){
		    albumService.addAlbum(album);	
		}else{
		    albumService.updateAlbum(album);
		}		
		return SUCCESS;
	}
	
	@Action(value = "delete_album", results = {@Result(name = SUCCESS, location = "gallery.jsp")})
	public String delete() throws Exception {
	    albumService.delAlbum(album);	
		return SUCCESS;
	}
	
	@Action(value = "gallery_index", results = {@Result(name = SUCCESS, location = "gallery.jsp")})
	public String index() throws Exception{
		return SUCCESS;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public List<Album> getAlbums() {
		return albums;
	}

	public Map getResult() {
		return result;
	}

    public List<AlbumDataGridModel> getAlbumsDataGrid() {
        return albumsDataGrid;
    }

    public List<ColumnComboTreeModel> getColumnsComboTree() {
        return columnsComboTree;
    }

	public void setGalleryTitle(String galleryTitle) {
		this.galleryTitle = galleryTitle;
	}

	public void setGalleryColumn(String galleryColumn) {
		this.galleryColumn = galleryColumn;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

    
    
}
